import tkinter as tk
from tkinter import ttk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

plt.rcParams['font.sans-serif'] = ['SimHei']  # 使用黑体
plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题
plt.rcParams['font.size'] = 14  # 设置全局字体大小
plt.rcParams['axes.titlesize'] = 16  # 设置标题字体大小
plt.rcParams['axes.labelsize'] = 14  # 设置坐标轴标签字体大小



class EconomicDataBrowser:
    def __init__(self, root):
        self.root = root
        self.root.title('经济数据浏览器')
        self.root.geometry('800x600')
        # 设置全局默认字体
        default_font = ('SimHei', 12)
        self.root.option_add('*Font', default_font)

        # 创建Treeview样式
        style = ttk.Style()
        style.configure('Large.Treeview', font=default_font)
        # 创建左侧面板
        left_panel = tk.Frame(root, width=200, bg='#f0f0f0')
        left_panel.pack(side=tk.LEFT, fill=tk.Y)

        # 指标树
        self.tree = ttk.Treeview(left_panel, style='Large.Treeview')
        self.tree['columns'] = ('value')
        self.tree.heading('#0', text='指标', anchor='w')
        self.tree.heading('value', text='-', anchor='w')
        self.tree.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)

        # 搜索框
        search_frame = tk.Frame(left_panel)
        search_frame.pack(fill=tk.X, padx=5, pady=5)
        self.search_entry = tk.Entry(search_frame, font=('Arial', 12))
        self.search_entry.pack(side=tk.LEFT, fill=tk.X, expand=True)
        search_btn = tk.Button(search_frame, text='搜索', font=('Arial', 12))
        search_btn.pack(side=tk.RIGHT)

        # 绘图按钮
        plot_frame = tk.Frame(left_panel)
        plot_frame.pack(fill=tk.X, padx=5, pady=5)
        self.plot_btn = tk.Button(plot_frame, text='绘图', command=self.plot_data, font=('Arial', 12))
        self.plot_btn.pack(fill=tk.X)

        # 季节性绘图按钮
        seasonal_plot_frame = tk.Frame(left_panel)
        seasonal_plot_frame.pack(fill=tk.X, padx=5, pady=5)
        self.seasonal_plot_btn = tk.Button(seasonal_plot_frame, text='季节性绘图', command=self.plot_seasonal_data, font=('Arial', 12))
        self.seasonal_plot_btn.pack(fill=tk.X)

        # 创建右侧绘图面板
        self.figure = plt.Figure(figsize=(5, 4), dpi=100)
        self.ax = self.figure.add_subplot(111)
        self.canvas = FigureCanvasTkAgg(self.figure, master=root)
        self.canvas.get_tk_widget().pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)

        # 初始化数据
        self.init_data()

    def plot_seasonal_data(self):
        selected = self.tree.focus()
        if not selected:
            return
        
        item = self.tree.item(selected)
        values = item['values']
        if not values or len(values) < 4:
            return
        
        self.ax.clear()
        self.ax.plot(values, marker='o')
        self.ax.set_title(item['text'] + ' (季节性)')
        self.ax.set_xlabel('季度')
        self.ax.set_ylabel('值')
        self.canvas.draw()

    def plot_data(self):
        selected = self.tree.focus()
        if not selected:
            return
        
        item = self.tree.item(selected)
        values = item['values']
        if not values:
            return
        print('selected,', selected, 'values', values)
        self.ax.clear()
        self.ax.plot(values, marker='o')
        self.ax.set_title(item['text'])
        self.ax.set_xlabel('时间')
        self.ax.set_ylabel('值')
        self.canvas.draw()

    def init_data(self):
        # 流量数据
        flow_data = self.tree.insert('', 'end', text='国家资金流量表')
        
        # 住户部门
        households = self.tree.insert(flow_data, 'end', text='住户')
        self.tree.insert(households, 'end', text='GDP', values=(100, 105, 110, 115, 120))
        self.tree.insert(households, 'end', text='CPI', values=(2.5, 2.6, 2.7, 2.8, 2.9))
        
        # 非金融企业
        firms = self.tree.insert(flow_data, 'end', text='非金融企业')
        self.tree.insert(firms, 'end', text='行业数据', values=(50, 55, 60, 65, 70))
        self.tree.insert(firms, 'end', text='区域数据', values=(60, 65, 70, 75, 80))
        
        # 金融部门
        fin_firms = self.tree.insert(flow_data, 'end', text='金融部门')
        self.tree.insert(fin_firms, 'end', text='企业数据', values=(30, 35, 40, 45, 50))
        self.tree.insert(fin_firms, 'end', text='个人数据', values=(40, 45, 50, 55, 60))
        
        # 广义政府
        government = self.tree.insert(flow_data, 'end', text='广义政府')
        self.tree.insert(government, 'end', text='经济预期', values=(3.0, 3.1, 3.2, 3.3, 3.4))
        self.tree.insert(government, 'end', text='市场预期', values=(2.8, 2.9, 3.0, 3.1, 3.2))
        
        # 国内合计
        domestic = self.tree.insert(flow_data, 'end', text='国内合计')
        
        # 存量数据
        specific_data = self.tree.insert('', 'end', text='国家资产负债表')
        
        # 产业用电
        industry = self.tree.insert(specific_data, 'end', text='产业用电')
        self.tree.insert(industry, 'end', text='第一产业', values=(10, 15, 20, 25, 30))
        self.tree.insert(industry, 'end', text='第二产业', values=(20, 25, 30, 35, 40))
        self.tree.insert(industry, 'end', text='第三产业', values=(30, 35, 40, 45, 50))
        
        # 行业用电
        sector = self.tree.insert(specific_data, 'end', text='行业用电')
        self.tree.insert(sector, 'end', text='制造业', values=(15, 20, 25, 30, 35))
        self.tree.insert(sector, 'end', text='服务业', values=(25, 30, 35, 40, 45))
        
        # 需求用电
        demand = self.tree.insert(specific_data, 'end', text='需求用电')
        self.tree.insert(demand, 'end', text='居民用电', values=(5, 10, 15, 20, 25))
        self.tree.insert(demand, 'end', text='商业用电', values=(10, 15, 20, 25, 30))
        
        # 微观主体用电
        micro_subject = self.tree.insert(specific_data, 'end', text='微观主体用电')
        self.tree.insert(micro_subject, 'end', text='企业A', values=(2, 3, 4, 5, 6))
        self.tree.insert(micro_subject, 'end', text='企业B', values=(3, 4, 5, 6, 7))

if __name__ == '__main__':
    root = tk.Tk()
    app = EconomicDataBrowser(root)
    root.mainloop()